package com.xdjk.networkdemo.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.LocationManager;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;

import com.xdjk.networkdemo.XApplication;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;



public class AppUtils {

    public static final String TAG = AppUtils.class.getSimpleName();


    /**
     * 获取当前APP版本号信息
     *
     * @return
     * @throws Exception
     */
    public static String getAppVersion() {

        // 获取packagemanager的实例
        PackageManager packageManager = XApplication.getInstance().getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(XApplication.getInstance().getPackageName(),
                    0);
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String version = "";

        if (packInfo != null) {
            version = packInfo.versionName;
        }

        return version;
    }


    /**
     * 获取系统版本号
     *
     * @return
     */
    public static String getOSVersion() {

        String osVersion = android.os.Build.VERSION.RELEASE;

        return osVersion;
    }

    /**
     * 获取手机IMEI
     *
     * @return
     */
    public static String getIMEI() {

        String imei = ((TelephonyManager) XApplication.getInstance().getSystemService(
                Context.TELEPHONY_SERVICE)).getDeviceId();

        return imei;

    }

    //过滤特殊字符
    public static boolean StringFilter(String str) throws PatternSyntaxException {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";

        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);

        return !m.find();
    }


    /**
     * 获取平台名称
     *
     * @return
     */
    public static String getPlatform() {

        return "android";

    }

    /**
     * 获取渠道ID
     * <p>
     * 00 表示商户App
     * 01 表示手刷App
     *
     * @return
     */
    public static String getChannelNo() {

        return "01";

    }


    /**
     * 判断GPS是否打开
     *
     * @return
     */
    public static boolean isGPSOpen(Context mContext) {

        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            return true;

        } else {

            return false;
        }

    }

    //获取屏幕的宽度
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    //获取屏幕的高度
    public static int getScreenHeight(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getHeight();
    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 读取baseurl
     * @param url
     * @return
     */
    public static String getBasUrl(String url) {
        url = "http://172.20.128.123:8080/UploadFileServer/uc.apk";
        String head = "";
        int index = url.indexOf("://");
        if (index != -1) {
            head = url.substring(0, index + 3);
            url = url.substring(index + 3);
        }
        index = url.indexOf("/");
        if (index != -1) {
            url = url.substring(0, index + 1);
        }
        return head + url;
    }
}
